package com.example.demo.util;

import junit.framework.TestCase;
import junit.textui.TestRunner;

public class TestGeneratePrimesV2 extends TestCase {

    public static void main(String[] args) {
        TestRunner.main(new String[]{"TestGeneratePrimes"});
    }

    public TestGeneratePrimesV2(String name){
        super(name);
    }

    public void testPrimes(){
        int[] nullArray = GeneratePrimes.generatePrimes(0);
        assertEquals(nullArray.length,0);
        int[] minArray = GeneratePrimes.generatePrimes(2);
        assertEquals(minArray.length,1);
        assertEquals(minArray[0],2);
        int[] threeArray = GeneratePrimes.generatePrimes(3);
        assertEquals(threeArray.length,2);
        assertEquals(threeArray[0],2);
        assertEquals(threeArray[1],3);

        int[] centArray = GeneratePrimes.generatePrimes(100);
        assertEquals(centArray.length,25);
        assertEquals(centArray[24],97);
    }

    public void testExhaustive(){
        for (int i = 2; i < 500; i++) {
            verifyPrimeList(PrimeGeneratorV5.generatePrimes(i));
        }
    }

    private void verifyPrimeList(int[] list){
        for (int i = 0; i < list.length; i++) {
            verifyPrime(list[i]);
        }
    }

    private void verifyPrime(int n){
        for (int factor = 2; factor < n; factor++) {
            assert(n % factor != 0);
        }
    }
}
